/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.3 $
 * * DATE
 * *    $Date: 2007/01/04 09:14:09 $
 * * LOG
 * *    $Log: PreBuiltAtagManager.java,v $
 * *    Revision 1.3  2007/01/04 09:14:09  lmottola
 * *    Changed constructor for AtagManager.
 * *
 * *    Revision 1.2  2006/12/14 22:26:37  lmottola
 * *    LNScope becomes an interface that includes matches().
 * *
 * *    Revision 1.1  2006/11/05 21:11:30  lmottola
 * *    Added pre-built AtagManager for traffic app.
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.apps.traffic;

import atag.common.ATaGProgram;
import atag.runtime.AtagManager;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;
import atag.runtime.RunnableTask;
import atag.runtime.ln.ConjunctiveScope;
import atag.runtime.ln.LNScope;
import atag.runtime.ln.predicates.IntegerRangePredicate;
import atag.runtime.ln.predicates.IntegerSimplePredicate;
import atag.runtime.ln.predicates.LNPredicate;
import atag.runtime.ln.predicates.SetMembershipPredicate;

public class PreBuiltAtagManager extends AtagManager {

	public PreBuiltAtagManager() {
		
	}
	
	public void setUp() {
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(0),
				new RampSampler(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 0));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(1),
				new SpeedSampler(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 1));
		taskDecls
				.add(new RunnableTask(this.m_program.getTaskList().get(2),
						new AverageQueueLengthCalculator(this.m_dataPool,
								this.m_config), Thread.MAX_PRIORITY - 2));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(3),
				new AverageSpeedCalculator(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 3));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(4),
				new SpeedLimitCalculator(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 4));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(5),
				new RGSignalCalculator(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 5));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(6),
				new SpeedLimitDisplayer(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 6));
		taskDecls.add(new RunnableTask(this.m_program.getTaskList().get(7),
				new RGSignalDisplayer(this.m_dataPool, this.m_config),
				Thread.MAX_PRIORITY - 7));
	}

	public LNScope[] getLNScopeForData(int taskID, int dataID) {

		int myHS = ((Integer) m_config.getAttributeByName("HS")).intValue();
		LNPredicate sameHighWaySector = new IntegerSimplePredicate("HS",
				IntegerSimplePredicate.EQUAL, myHS);
		LNPredicate nearbyHighWaySector = new IntegerRangePredicate("HS",
				myHS - 1, myHS + 1);

		switch (dataID) {

		case IDConstants.D_VPRESENCE:
			// set LNScope to: node in same HS with taskID 2 running
			LNPredicate qCalculatorInstantiated = new SetMembershipPredicate(
					new Integer(IDConstants.T_AQLENCALCULATOR),
					SetMembershipPredicate.IS_IN,
					NodeInfo.ASSIGNED_TASK_ATTR_NAME);
			return new LNScope[] { new ConjunctiveScope(new LNPredicate[] {
					sameHighWaySector, qCalculatorInstantiated }) };

		case IDConstants.D_RSPEED:
			// set LNScope to: node in same HS with taskID 3 running
			LNPredicate sCalculatorInstantiated = new SetMembershipPredicate(
					new Integer(IDConstants.T_ASCALCULATOR),
					SetMembershipPredicate.IS_IN,
					NodeInfo.ASSIGNED_TASK_ATTR_NAME);
			return new LNScope[] { new ConjunctiveScope(new LNPredicate[] {
					sameHighWaySector, sCalculatorInstantiated }) };

		case IDConstants.D_AQLENGTH:
		case IDConstants.D_ASPEED:
			// set LNScope to: node in same HS+-1 with taskID 4 or 5 running
			LNPredicate sLimitInstantiated = new SetMembershipPredicate(
					new Integer(IDConstants.T_SLCALCULATOR),
					SetMembershipPredicate.IS_IN,
					NodeInfo.ASSIGNED_TASK_ATTR_NAME);
			LNPredicate tSignInstantiated = new SetMembershipPredicate(
					new Integer(IDConstants.T_RGSCALCULATOR),
					SetMembershipPredicate.IS_IN,
					NodeInfo.ASSIGNED_TASK_ATTR_NAME);
			return new LNScope[] {
					new ConjunctiveScope(new LNPredicate[] {
							nearbyHighWaySector, sLimitInstantiated }),
					new ConjunctiveScope(new LNPredicate[] {
							nearbyHighWaySector, tSignInstantiated }) };

		case IDConstants.D_SLIMIT:
			// set LNScope to: node in same HS with speedDisplayer in
			// attachedActuators
			LNPredicate speedDisplayerAttached = new SetMembershipPredicate(
					"SpeedLimitDisplayer", SetMembershipPredicate.IS_IN,
					"attachedActuators");
			return new LNScope[] { new ConjunctiveScope(new LNPredicate[] {
					sameHighWaySector, speedDisplayerAttached }) };

		case IDConstants.D_RGSIGNAL:
			// set LNScope to: node in same HS with RGSignalDisplayer in
			// attachedActuators
			LNPredicate RGSignalDisplayerAttached = new SetMembershipPredicate(
					"RGSignalDisplayer", SetMembershipPredicate.IS_IN,
					"attachedActuators");
			return new LNScope[] { new ConjunctiveScope(new LNPredicate[] {
					sameHighWaySector, RGSignalDisplayerAttached }) };

		default:
			return new LNScope[] {};
		}
	}
}
